package com.powernode.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.domain.SysUser;
import com.powernode.model.Result;
import com.powernode.service.SysUserService;
import com.powernode.util.AuthUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统用户业务控制层
 */
@Api(tags = "系统用户业务接口管理")
@RequestMapping("sys/user")
@RestController
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @ApiOperation("查询登录用户信息")
    @GetMapping("info")
    public Result<SysUser> loadSysUserInfo() {
        // 获取用户标识
        Long userId = AuthUtils.getLoginUserId();
        // 根据用户标识查询用户详情
        SysUser sysUser = sysUserService.getById(userId);
        return Result.success(sysUser);
    }

    @ApiOperation("多条件分页查询管理员列表")
    @GetMapping("page")
    @PreAuthorize("hasAuthority('sys:user:page')")
    /*public Result<Page<SysUser>> loadSysUserPage(@RequestParam(defaultValue = "1") Long current,
                                                 @RequestParam(defaultValue = "10") Long size,
                                                 @RequestParam(required = false) String username) {*/
    public Result<Page<SysUser>> loadSysUserPage(Page<SysUser> page,String username) {
        // 多条件分页查询管理员列表
        /*page = sysUserService.page(page,new QueryWrapper<SysUser>()
                *//*<if test="username != null and username != ''">
                    username like '%'xx'%'
                </if>*//*
                .like(StringUtils.hasText(username),"username",username)
                .orderByDesc("create_time")
        );*/
        page = sysUserService.page(page,new LambdaQueryWrapper<SysUser>()
                .like(StringUtils.hasText(username),SysUser::getUsername,username)
                .orderByDesc(SysUser::getCreateTime)
        );
        return Result.success(page);
    }

    @ApiOperation("新增管理员")
    @PostMapping
    @PreAuthorize("hasAuthority('sys:user:save')")
    public Result<String> addSysUser(@RequestBody SysUser sysUser) {
        Integer count = sysUserService.addSysUser(sysUser);
        return Result.handle(count>0);
    }

    @ApiOperation("查询管理员详情")
    @GetMapping("info/{id}")
    @PreAuthorize("hasAuthority('sys:user:info')")
    public Result<SysUser> loadSysUserDetail(@PathVariable Long id) {
        SysUser sysUser = sysUserService.querySysUserDetailById(id);
        return Result.success(sysUser);
    }

    @ApiOperation("修改管理员信息")
    @PutMapping
    @PreAuthorize("hasAuthority('sys:user:update')")
    public Result<String> modifySysUser(@RequestBody SysUser sysUser) {
        Integer count = sysUserService.modifySysUser(sysUser);
        return Result.handle(count>0);
    }

    @ApiOperation("批量删除管理员")
    @DeleteMapping("{userIds}")
    @PreAuthorize("hasAuthority('sys:user:delete')")
    public Result<String> removeSysUserList(@PathVariable List<Long> userIds) {
        Boolean flag = sysUserService.removeSysUserByIds(userIds);
        return Result.handle(flag);
    }
}

